<?php

/**
 * question actions.
 *
 * @package    samsung
 * @subpackage question
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class questionActions extends sfActions
{
 /**
  * Executes list action
  *
  * @param sfRequest $request A request object
  */
  public function executeList(sfWebRequest $request) {
    if (!$request->hasParameter('course_id')) {
      $this->forward404();
    }
    $this->questions = QUESTIONTable::getInstance()->getByCourseId($request->getParameter('course_id'), 0, true);
    $this->arrStatusList    = StringUtil::getStatusArray();

    $arrCourseList    = COURSETable::getInstance()->getCoursesArray();
    $this->courseName    = $arrCourseList[$request->getParameter('course_id')];
    $this->courseId    = $request->getParameter('course_id');
  }

  public function executeAdd(sfWebRequest $request) {
    $errorCollector = array();
    $this->form = new QuestionForm();
    if ($request->hasParameter('course_id')) {
      $this->form->setDefault(SlideForm::FIELD_COURSE, $request->getParameter('course_id'));
    }
    $this->isEdit = false;

    if ($request->hasParameter('id')) {
      $this->isEdit = true;
      $question = QUESTIONTable::getInstance()->find($request->getParameter('id'));
      $this->form->populateData($question);
      if (!$question instanceof QUESTION) {
        $this->isEdit = false;
      }
    }
    try {
      if ($request->isMethod('post')) {
        $formData = $request->getParameter(QuestionForm::FORM_NAME);
        $this->form->bind($formData);

        if ($this->form->isValid()) {
          if (!$this->isEdit) {
            $question = new QUESTION();
            $lineNo = QUESTIONTable::getInstance()->getMaxId() + 1;
            $question->setLineNo($lineNo);
          }
          $question->setQuestion($formData[QuestionForm::FIELD_QUESTION]);
          $question->setCourseId($formData[QuestionForm::FIELD_COURSE]);
          $question->setIsActive($formData[QuestionForm::FIELD_STATUS]);
          $question->save();

          $this->redirect('@question-list?course_id='.$formData[SlideForm::FIELD_COURSE]);
        }
      }
    } catch (Exception $e) {
      $errorCollector[] = 'Internal error!.. Please try again';
    }
    $this->errorCollector = $errorCollector;
  }

  public function executeDelete(sfWebRequest $request) {
    $question = QUESTIONTable::getInstance()->find($request->getParameter('id'));
    if (!$question instanceof QUESTION) {
      $this->forward404();
    }
    try {
      $question->setIsActive(Constants::STATUS_DELETED);
      $question->save();

      $this->redirect('@question-list?course_id='.$question->getCourseId());
    } catch (Exception $e) {

    }
  }
  
}
